import sDate from "./date";
import szdRouter from "../utils/goRouter";
import { range } from "./range";
import { lockOff, lockOn } from "./lock";
import { $m, $t } from "../utils/globalConfig";

/**
 * 下载文件(字节流方式)
 * @param {Blob} fileStream 文件流
 * @param {String} fileType 文件类型
 * @param {String} fileName 下载的文件名称
 */
function downloadFile(fileStream, fileType, fileName) {
  // 利用a标签自定义下载文件名
  const link = document.createElement("a");
  // 创建Blob对象，设置文件类型
  let blob = new Blob([fileStream], { type: `application/${fileType}` });
  let objectUrl = URL.createObjectURL(blob); // 创建URL
  link.href = objectUrl;
  link.download = fileName || sDate.format(undefined, "YYYYMMDDHHmmss"); // 自定义文件名
  link.click(); // 下载文件
  URL.revokeObjectURL(objectUrl); // 释放内存
  $m("CORE_CLIENT.S010"); //导出文件成功
}

/**
 * 构造树型结构数据
 * @param {Array} data 数据源
 * @param {String} id id字段
 * @param {String} parentId 父节点字段
 * @param {String} children 子节点字段
 * @param {String} rootId 根Id
 */
function getTree(data, id = "id", parentId = "parentId", children = "children", rootId = "") {
  id = id || "id";
  parentId = parentId || "parentId";
  children = children || "children";
  rootId = rootId || "";
  //对源数据深度克隆
  const cloneData = JSON.parse(JSON.stringify(data));
  //循环所有项
  const treeData = cloneData.filter(father => {
    let branchArr = cloneData.filter(child => {
      //返回每一项的子级数组
      return father[id] === child[parentId];
    });
    branchArr.length > 0 ? (father.children = branchArr) : "";
    //返回第一层
    return father[parentId] === rootId;
  });
  return treeData != "" ? treeData : data;
}

/**
 * 获取联动最终值
 * @param {*} cascaders 联动选择的值
 * @return {String} 返回周后一个节点
 * */
function getCascader(cascaders) {
  if (cascaders && Array.isArray(cascaders)) {
    if (cascaders.length === 0) {
      return "";
    } else {
      return cascaders[cascaders.length - 1];
    }
  } else {
    return cascaders;
  }
}

/**
 * 复制Excel粘贴
 * @return {Array} 返回表数据，列数据通过"\t"分割
 */
async function getPaste() {
  const res = await navigator.clipboard.readText();
  return res
    .replace(/"((?:[^"]*(?:\r\n|\n\r|\n|\r))+[^"]+)"/gm, function (match, p1) {
      return p1.replace(/""/g, '"').replace(/\r\n|\n\r|\n|\r/g, " ");
    })
    .split(/\r\n|\n\r|\n|\r/g);
}

/**
 * 标题操作文本
 * @param option 操作类型 C新增 U更改 V显示
 * @return {String} 返回操作描述
 */
function getTitle(option) {
  if (option === "C") return $t("新增");
  if (option === "U") return $t("更改");
  if (option === "V") return $t("显示");
  return "";
}

/**
 * 转驼峰
 * @param {String} str
 */
function toCamelCase(str) {
  return str.replace(/[-_](\w)/g, (_, c) => c.toUpperCase());
}

/**
 * 帮助文档
 * @param {String} docId 帮助文档id 命名规则：建议路由代码+表+字段 或 表+字段
 */
function helpDoc(docId) {
  szdRouter.goNavDg("SKIS_05B", { docId: docId });
}

/**
 * 常用文件图标
 * @return {Object} 返回常用图片对象
 */
function getFileTypeIcon() {
  return {
    pdf: require("../../assets/icons/pdf.png"),
    doc: require("../../assets/icons/doc.png"),
    docx: require("../../assets/icons/doc.png"),
    xls: require("../../assets/icons/xls.png"),
    xlsx: require("../../assets/icons/xls.png"),
    ppt: require("../../assets/icons/ppt.png"),
    pptx: require("../../assets/icons/ppt.png"),
    csv: require("../../assets/icons/csv.png"),
    html: require("../../assets/icons/html.png"),
    txt: require("../../assets/icons/txt.png"),
    xml: require("../../assets/icons/xml.png"),
    obj: require("../../assets/icons/txt.png")
  };
}

const szdTool = {
  range,
  downloadFile,
  getTree,
  getCascader,
  getPaste,
  getTitle,
  getFileTypeIcon,
  toCamelCase,
  helpDoc,
  lockOn,
  lockOff
};

export default szdTool;
